#!/bin/bash -i
srcPath=/data/program/go/src/ubt-rbac-service
dstPath=/opt/apps/ubt-rbac
appName=ubt-rbac-service
echo "Build the project Begin!"
echo "Go into the srouce path of project directory: $srcPath"
cd $srcPath
echo "Update the New Program."
git pull
echo "Check and update the go libs."
go mod tidy
echo "Build the go project."
go build main.go
echo "Rename the executed file."
mv main $appName
echo "Kill the old proccess."
PS=$(ps -ef|grep $appName|grep -v grep|awk '{print $2}')
if [ "$PS" != "" ]
then
  kill -9 $PS
  echo "$PS proccess killed."
fi
sleep 1
echo "Delete the executed file."
rm -rf $dstPath/$appName
echo "Move the executed file to $dstPath"
mv $appName $dstPath
echo "Go into the executed directory: $dstPath."
cd $dstPath
echo "Execute the file."
nohup ./$appName &
echo "Build the project finished!"

